<?php

    namespace Sale\Controller;

    use Common\Controller\AdminbaseController;

    class AdminSaleController extends AdminbaseController {

        protected $m_card,$m_sale,$m_card_sale,$m_sale_good;

        function _initialize() {
            parent::_initialize();
            $this->m_card= D("Admin/Card");
            $this->m_sale= D("Logistics/Sale");
            $this->m_card_sale= D("Sale/CardSale");
            $this->m_sale_good= D("Sale/SaleGood");
        }


        /**
         * 消费
         */
        public function add(){
            // $card_list = $this->m_card->where()->select();
            $card_list = array();
            $this->assign("card_list",$card_list);
            $sale_id = get_sale_id();
            $good_list = $this->m_sale_good->where(array("sale_id"=>$sale_id))->select();
            $this->assign("good_list",$good_list);
            $sale = get_sale_data();
            $this->assign("sale",$sale);
            $admin_oil_discount = get_config_by_key('admin_oil_discount');
            $this->assign("admin_oil_discount",$admin_oil_discount);
            $this->display();
        }

        /**
         * 会员卡消费预计算
         * @return [type] [description]
         */
        public function pre_post(){
            @header('Content-type: application/json');
            $sale_id = get_sale_id();   // 消费机构ID
            $good_id = I("post.good_id");   // 商品ID
            $card_number = I("post.card_number");   // 商品ID
            $goods_price = I("post.goods_price");   // 收费价格
            $good_num = I("post.good_num"); // 商品数量
            $last_key = I("post.last_key"); // 最后修改的数据类型:preprice|good_num

            $result = $this->m_card_sale->pre_clac($sale_id, $good_id, $goods_price, $good_num, $last_key, $card_number);
            if($result === -1){
                exit($this->error('会员卡信息获取失败！'));
            }elseif($result === -2){
                exit($this->error('会员卡余额不足！'));
            }elseif($result === -3){
                exit($this->error('该会员卡已注销，无法消费！'));
            }elseif($result === -4){
                exit($this->error('上级消费机构余额不足，无法消费！'));
            }elseif($result === -5){
                exit($this->error('商品信息获取失败！'));
            }
            exit(json_encode(array('status'=>'1','data'=>$result)));
        }

        /**
         * 会员卡消费提交
         */
        public function add_post(){
            if (IS_POST) {
                $card_number = I("post.card_number");
                $logistics_name = I("post.logistics_name");
                $card_balance = I("post.card_balance");
                $preprice = I("post.preprice");
                $good_num = I("post.good_num");
                $sale_good_id = I("post.sale_good_id");
                $force_price = I("post.force_price");

                $vip_name = I("post.vip_name");
                $vip_phone = I("post.vip_phone");
                $vip_plate_number = I("post.vip_plate_number");

                $card_update = array(
                    'vip_name' => $vip_name,
                    'vip_phone' => $vip_phone,
                    'vip_plate_number' => $vip_plate_number,
                );


                $desc = I("post.desc");
                if(empty($sale_good_id)){
                    $this->error("请先选择商品！");exit;
                }
                if(empty($card_number) || empty($logistics_name) || empty($card_balance)){
                    $this->error("请先扫卡！");exit;
                }
                if(empty($good_num)){
                    $this->error("请输入商品数量！");exit;
                }
                $sale_good = $this->m_sale_good->where(array("id"=>$sale_good_id))->find();
                if(empty($sale_good)){
                    $this->error("您选择的商品不存在，请重新选择！");exit;
                }
                $sale_id = get_sale_id();
                $result = $this->m_card_sale->insert_data($sale_id, $card_number, $preprice, $good_num, $desc, $sale_good, $force_price);

                if($result === -1){
                    $this->error("会员卡信息获取失败！");exit;
                }elseif($result === -2){
                    $this->error("会员卡余额不足！");exit;
                }elseif($result === -3){
                    $this->error("上级消费机构余额不足，无法消费！");exit;
                }elseif($result === -4){
                    $this->error("该会员卡已注销，无法消费！");exit;
                }elseif($result === -5){
                    $this->error("商品信息获取失败！");exit;
                }elseif($result === -6){
                    $this->error("消费机构查询失败，请重新登录！");exit;
                }
                if ($result) {
                    $card_info = $this->m_card->where(array('number'=>$card_number))->save($card_update);
                    push_sale($result);
                    redirect("/index.php?g=Sale&m=AdminSale&a=sale_print&id=".$result);
                    // $this->success("消费成功！");
                } else {
                    $this->error("消费失败！");
                }
            }
        }

        /**
         * 会员卡消费提交撤销
         */
        public function cancel_post(){
            $id = I("get.id");
            $card_number = I("get.card_number");
            $price = I("get.price");

            $card_info = $this->m_card->where(array('number'=>$card_number))->find();

            $card_sale_info = $this->m_card_sale->where(array('number'=>$card_number))->find();

            $result = $this->m_card_sale->delete_data($id);

            if($result === -1){
                $this->error("此订单不存在或已被撤销！");exit;
            }elseif($result === -2){
                $this->error("此卡号不存在");exit;
            }
            
            if ($result) {
                $this->success("撤销成功！");
            } else {
                $this->error("撤销成功！");
            }
        }

        public function sale_print(){
            $id = I("get.id");
            
            $card_sale_data = $this->m_card_sale->where(array("id"=>$id))->find();
            $card_sale_data['total_add_point'] = $card_sale_data['general_point'] + $card_sale_data['self_point'];
            $this->assign("sale_data",$card_sale_data);
            
            $card_data = $this->m_card->where(array('id'=>$card_sale_data['cid']))->find();
            $this->assign("card_data",$card_data);

            $sale = M("Sale")->where(array("id"=>$card_sale_data['sale_id']))->find();
            $this->assign("sale",$sale);

            $sale_user = M('users')->where(array('id'=>$card_sale_data['uid']))->find();
            $this->assign("sale_user",$sale_user);
            $this->display();
        }

    }